package com.leenleda.ips.activity;

import androidx.databinding.DataBindingUtil;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import com.leenleda.ips.BaseActivity;
import com.leenleda.ips.MyApplication;
import com.leenleda.ips.R;
import com.leenleda.ips.databinding.ActivityLoginDataBinding;
import com.leenleda.ips.entity.BaseEntity;
import com.leenleda.ips.entity.DataEntity;
import com.leenleda.ips.receiver.Constant;
import com.leenleda.ips.util.BaseUtils;
import com.leenleda.ips.util.FileData;
import com.leenleda.ips.util.FileUtils;
import com.leenleda.ips.util.NetUtils;
import com.leenleda.ips.util.TimeUtils;
import com.leenleda.ips.util.UShared;
import com.leenleda.ips.util.Utils;

import java.net.InetAddress;

public class LoginDataActivity extends BaseActivity {

    @Override
    public void conTrollShow(int state, String ... data) {
    }

    private ActivityLoginDataBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView( R.layout.activity_login_data);
        binding = (ActivityLoginDataBinding) bindingView;
//        binding = DataBindingUtil.setContentView(this, R.layout.activity_login_data);
        getVersion();
        initBaseView();
        findViewById(R.id.snap).setOnClickListener(v -> {//请李飞到产科201号诊室就诊
            finish();
        });
    }

    private void initBaseView() {
        long moreTime = UShared.getLong("moreTime", 0);
        long sysTime = System.currentTimeMillis();
        long cuTime = moreTime + sysTime;
        long serverDay = TimeUtils.getDaySecond(cuTime);

        binding.timeLocal.setText(TimeUtils.stampToDate(sysTime));
        binding.timeCurrent.setText(TimeUtils.stampToDate(cuTime));
        binding.timeZero.setText(UShared.getIntValue("time_left") + "");
//        binding.timeZero.setText(Utils.stampToDate(serverDay) + "---" + serverDay);
        binding.timeOn.setText(TimeUtils.stampToDate(UShared.getLong("onTimeD", 0)));
        binding.timeDown.setText(TimeUtils.stampToDate(UShared.getLong("offTimeD", 0)));

        InetAddress address = NetUtils.getLocalIPAddress();
        String str = String.format("%s(v_%s),  本机IP:%s,  剩余可用连接次数:%s", versionCode, version, address.getHostAddress(), Constant.AUTHOR_TIME);
        binding.appVersion.setText(str);
//        binding.title.setText(String.format("(设备基本信息 本机IP:%s 剩余可用连接次数:%s)", address.getHostAddress(), info.getHistoryNum()) );

        DataEntity info = FileData.getDeviceInfo(5);
        if (info == null)
            return;
        binding.voiceNum.setText(info.getVolume() + "/max (当前视频缓存大小" + Constant.cacheSize + ")");
        binding.urlWork.setText(info.getMediaUrl());
//        BaseEntity base = BaseUtils.getBase();
        binding.urlServer.setText(Utils.getStringValue(FileUtils.getServiceIp()));
        binding.deviceId.setText(FileUtils.getDeviceMac());
    }

    private String version = "";
    private int versionCode = 0;
    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            version = info.versionName;
            versionCode = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "无法获取到版本号";
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
